package com.qy139.store.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qy139.entity.Room;
import com.qy139.entity.Seat;
import com.qy139.store.mapper.RoomMapper;
import com.qy139.store.mapper.SeatMapper;
import com.qy139.store.service.IRoomService;
import com.qy139.store.service.ISeatService;
import com.qy139.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hjl
 * @since 2021-12-01
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Resource
    private RoomMapper roomMapper;
    @Resource
    private ISeatService seatService;
    @Resource
    private SeatMapper seatMapper;
    @Override
    public IPage<Room> selectAll(Page<Room> page, String rname) {
        return roomMapper.selectAll(page, rname);
    }

    @Override
    public Result selectName(Room room) {
        return new Result(roomMapper.selectName(room));
    }

    @Override
    public Result addRoomOrUpdate(Room room) {
        //计数sql 获得修改前的座位数
        Map map=seatMapper.countSeat(room.getId());
        int num=Integer.parseInt(map.get("seatcount").toString());
        //修改的时间
        room.setUpdatetime(new Date());
        //新输入的座位数
        int count= room.getRCount();
        //昵称
        String nick=room.getNickname();
        if (room.getId()==null){
            boolean b = this.saveOrUpdate(room);// id 自动增长  添加之后获取到对应的id的值
            int id=room.getId();
            List<Seat> seatList = new ArrayList<>();
            for (int i=1;i<=count;i++){
                Seat seat=new Seat();
                if (i<10){
                    seat.setSeatId(nick+"--0"+i);
                }else {
                    seat.setSeatId(nick+"--"+i);
                }
                seat.setRoomId(id);
                seatList.add(seat);
            }
            // 批量添加 座位
            boolean b1 = seatService.saveBatch(seatList);
            return new Result(b&b1);
        }else {
            if (count<num){
                return new Result(this.saveOrUpdate(room));
            }else {
                List<Seat> seatList1 = new ArrayList<>();
                int id=room.getId();
                //从原本的座位数后开始添加
                for (int q=num+1;q<=count;q++) {
                    Seat seat1 = new Seat();
                    if (q < 10) {
                        seat1.setSeatId(nick + "--0" + q);
                    } else {
                        seat1.setSeatId(nick + "--" + q);
                    }
                    seat1.setRoomId(id);
                    seatList1.add(seat1);
                }
                //批量添加座位
                Boolean a=seatService.saveBatch(seatList1);
                //新增或修改
                Boolean a1=this.saveOrUpdate(room);
                return new Result(a&a1);
            }
        }
    }

    @Override
    public Result updateSta(Room room) {
        //当前自习室的状态
        Integer status = room.getStatus();
        //当前更改状态的门店id
        Integer id = room.getId();
        List<Seat> list = seatService.list();
        for (Seat seat : list) {
            //进行判断当前点击的门店名字和座位门店名字是否相等
            if (seat.getRoomId().equals(id)){
                // 遍历出来的 status的值如果为0 更改为1
                if(status.equals(1)) {
                    seatService.updSta(id);
                }else{
                    seatService.updStb(id);
                }
            }
        }
        return new Result(this.saveOrUpdate(room));
    }
}
